package cn.anecansaitin.industrialization_expansion.common.item;

import cn.anecansaitin.industrialization_expansion.ModConstant;
import cn.anecansaitin.industrialization_expansion.common.ToolModuleType;
import cn.anecansaitin.industrialization_expansion.common.TranslationText;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

public class ModuleTemplate extends Item {
    private final ITextComponent necessary = new TranslationTextComponent("text." + ModConstant.MOD_ID + ".template.necessary").withStyle(TextFormatting.GOLD);
    private final ITextComponent optional = new TranslationTextComponent("text." + ModConstant.MOD_ID + ".template.optional").withStyle(TextFormatting.DARK_AQUA);
    private final ITextComponent core = new TranslationTextComponent("text." + ModConstant.MOD_ID + ".template.core").withStyle(TextFormatting.RED);
    private final ITextComponent coreTip = new TranslationTextComponent("text." + ModConstant.MOD_ID + ".template.core.tip").withStyle(TextFormatting.RED);

    public ModuleTemplate() {
        super(new Properties());
    }

    @Override
    public void appendHoverText(ItemStack stack, @Nullable World world, @Nonnull List<ITextComponent> tooltip, @Nonnull ITooltipFlag flag) {
        CompoundNBT nbt = stack.getOrCreateTag();

        if (nbt.contains("necessary")) {
            tooltip.add(nbt.getBoolean("necessary") ? necessary : optional);
        }

        if (nbt.contains("core") && nbt.getBoolean("core")) {
            tooltip.add(core);
            tooltip.add(new StringTextComponent(""));
            tooltip.add(coreTip);
        }
    }

    @Override
    public @Nonnull ITextComponent getName(ItemStack stack) {
        CompoundNBT nbt = stack.getOrCreateTag();

        if (nbt.contains("type"))
            return TranslationText.getModuleType(nbt.getString("type"));
        else return super.getName(stack);
    }

    public void setName(ItemStack stack, ToolModuleType type) {
        CompoundNBT nbt = stack.getOrCreateTag();
        nbt.putString("type", type.getName());
    }

    public void setNecessary(ItemStack stack, boolean necessary) {
        CompoundNBT nbt = stack.getOrCreateTag();
        nbt.putBoolean("necessary", necessary);
    }

    public void setCore(ItemStack stack, boolean core) {
        CompoundNBT nbt = stack.getOrCreateTag();
        nbt.putBoolean("core", core);
    }
}
